<?php
//============================================================+
// File name   : ModXinstallc.php
// Begin       : 2006-10-18
// Last Update : 2007-01-21
//                                                             
// Description : Reads structured modx data from an 
//               XML file and readies it for install.
// Platform    : PHP 5
//
// Author: Travis Baker 
// Todo: add lang support, add theme support,
//       add missing code, add support for sql tags, add 
//			 support for file tags                                           
//============================================================+

			
			
class ModXinstallc
{	
		public static $modData = array();






	 function simplexml2ISOarray($xml,$attribsAsElements=0) {
   if (get_class($xml) == 'SimpleXMLElement') {
       $attributes = $xml->attributes();
       foreach($attributes as $k=>$v) {
           if ($v) $a[$k] = (string) $v;
       }
       $x = $xml;
       $xml = get_object_vars($xml);
   }
   if (is_array($xml)) {
       if (count($xml) == 0) return (string) $x; // for CDATA
       foreach($xml as $key=>$value) {
           $r[$key] = $this->simplexml2ISOarray($value,$attribsAsElements);
           if (!is_array($r[$key])) $r[$key] = utf8_decode($r[$key]);
       }
       if (isset($a)) {
           if($attribsAsElements) {
               $r = array_merge($a,$r);
           } else {
               $r['@'] = $a; // Attributes
           }
       }
       return $r;
   }
   return (string) $xml;
}


  private function setData($data)
  {
    $this->modData = $data;
  }

  private function getData()
  {
    return $this->modData;
  }

	function install($zipFilename)
		{			
			$z = new ZipArchive();

    if ($z->open("../mods/zipedMods/".$zipFilename, ZIPARCHIVE::CREATE)!==TRUE) 
    {
         exit("cannot open <$zipFilename>\n");
    }
			   if($z->getFromName('info.xml'))   //if there is a info.xml file in the zip
			   {
			   
			   
			     //the mod can be extracted now
			     $z->extractTo("../mods/".$zipFilename);
			     
			     //convert to simplexml
			     $xmlfile = simplexml_load_file("../mods/".$zipFilename."/info.xml");
			     
           //convert simplexml to array
           $this->setData($this->simplexml2ISOarray($xmlfile));
			     
			     
			     //now we can rename the mod folder to its proper name with the getTitle function
			     rename("../mods/".$zipFilename, "../mods/".$this->getTitle());
			     
			     
			     
           //echo for debug purposes
			     echo "Mod Name: ".$this->getTitle()."<br/>Author: ".$this->getAuthorUserName()."<br/>Version: ".$this->getVersion()."<br/>Description: ".$this->getDescription();
			     //     ^should output info on mod^ 
			     
			     //copyFiles();
         }
         else
         {
          //echo for debug purposes
          echo 'the xml file for this mod is missing.';
         }
        
	 	}//end install
	 
	 
	 
	 ////////////////////////////
	 // functions to access data
	 //
	 //
	 ///////////////////////////
	 
	 //gets name of the mod
	 function getTitle()
	 {
	  $modData = $this->getData();
	 	return $modData['@']['modname'];
	 }
	 
	 //gets author name
	 function getAuthorUserName()
	 {
	 $modData = $this->getData();
	 	return $modData['author'];
	 }
	 
	 //gets the version of the mod
	 function getVersion()
	 {
	 $modData = $this->getData();
	 	return $modData['@attributes']['version'];
	 }
	 
	 //gets the description of the mod
	 function getDescription()
	 {
	 $modData = $this->getData();
	 	return $modData['description'];
	 }	 
	 
	 //gets PCM version required to install this mod
	 //need to impliment
	 function getRequiredTargetBuild()
	 {
	 $modData = $this->getData();
	  return;
	 }


    //$copyType is the method of copying
    //  1 = install
    //  2 = update
    //  3 = uninstall
    //
    // @info: defaults to install automaticly
	 function copyFiles($copyType = 1)
	{
	$modData = $this->getData();
	 if($copyType == 1)
	 {
		  foreach($modData['mod']['files']['file'] as $fileToCopy)
		  {
		  	//check xml file vs actual contents
		  	//if same then copy
		  }
		  foreach($modData['mod']['files']['img'] as $imgToCopy)
	   	{
		  	//check xml file vs actual contents
		  	//if same then copy
		  }
	 }
	 elseif($copyType == 2)
	 {
	   	foreach($modData['mod']['files']['file'] as $fileToCopy)
		  {
		  	//check xml file vs actual contents
		  	//if both have then check file version
		  	//if file that is in the ModX file has a bigger version number then update
		  	//otherwise ignore it
		  }
		  foreach($modData['mod']['files']['img'] as $imgToCopy)
	   	{
		  	//check xml file vs actual contents
		  	//if same then check file size
		  	//if different then update to the one in the ModX file
		  	//otherwise ignore it
		  }
   }
   elseif($copyType == 3)
	 {
	     //uninstall code here
	 }
}
	 
}//end class



//test here

$t = new ModXinstallc();
$t->install('someMod.zip')






?>
